package com.hl.card.pay.controller;

import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.plugins.Page;
import com.hl.card.common.persistence.model.DisMemberInfo;
import com.hl.card.common.persistence.model.Recharge;
import com.hl.card.common.util.ResponseModel;
import com.hl.card.modular.pack.service.DisMemberInfoService;
import com.hl.card.modular.pack.service.RechargeService;
import com.hl.card.pay.service.ChangeService;
import com.hl.card.pay.service.CommonService;
import com.hl.card.pay.vo.ChangeVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.HashMap;
import java.util.Map;

/**
 *兑换
 */
@Controller
public class ExChangeController {

    @Autowired
    private ChangeService changeService;

    @Autowired
    private CommonService commonService;

    @Autowired
    private RechargeService rechargeService;

    @Autowired
    private DisMemberInfoService disMemberInfoService;


    @RequestMapping("exChangeTest")
    public String transTest(){
        return "/change/changeTest.html";
    }
    @RequestMapping("exChange")
    public String change(){
        System.out.println("hello,change!");
        return "/change/changeTest.html";
    }
    @RequestMapping("doExChange")
    public String doChange(){
        return "/change/ExChangeTest.html";
    }

    @RequestMapping(value = "doExChangeApi",method = RequestMethod.POST)
    @ResponseBody
    public ResponseModel doChangeApi(ChangeVo changeVo){
        ResponseModel responseModel=new ResponseModel();
        int times= commonService.findPayError(changeVo.getUserId());
        boolean b=commonService.checkPayPassWord(changeVo.getUserId(),changeVo.getPayPassWord());
        DisMemberInfo disMember = disMemberInfoService.selectDisMemberInfo(changeVo.getUserId(), null);

        if(!changeService.checkExchange(changeVo.getUserId(), changeVo.getAmount())) {
            JSONObject jsonObject=new JSONObject();
            jsonObject.put("msg","请确认是否通过实名认证或者兑换超额");
            jsonObject.put("errorTimes",times+1);
            responseModel.setStatus("T0003");
            responseModel.setData(jsonObject);
            return responseModel;
        }
        if(times>4){
            responseModel.setStatus("T0001");
            responseModel.setData("密码输错次数超过上限！明天再试");
            return responseModel;
        }
        ResponseModel checkModel=commonService.checkChangeDto(changeVo);
        if(disMember==null&&!checkModel.getStatus().equals("T0000")){//非会员进行限额检查
            return checkModel;
        }

        if(b){

            String str=changeService.doChange(changeVo);
            JSONObject jsonObject = JSONObject.parseObject(str);
            if(jsonObject.get("errCode").equals("0000")){
                responseModel.setStatus("T0000");
            }else{
                responseModel.setStatus("T0006");
            }
            responseModel.setData(jsonObject);


            if(disMember==null){//非会员设置限额
                commonService.setChangeDatLimit(changeVo);
            }

        }else{
            JSONObject jsonObject=new JSONObject();
            jsonObject.put("msg","交易密码错误");
            jsonObject.put("errorTimes",times+1);
            responseModel.setStatus("T0002");
            responseModel.setData(jsonObject);
        }
        return  responseModel;
    }
    @RequestMapping("exChangeList")
    @ResponseBody
    public ResponseModel CollectList(@RequestParam(name = "userId") String userId, int size, int current){
        ResponseModel responseModel=new ResponseModel();
        Recharge recharge=new Recharge();
        recharge.setUser_id(userId);
        recharge.setExtra("EXCHANGE");
        Page<Recharge> dPage = rechargeService.selectMultiConditionLimit(recharge,size,current);
        Map<String, Object> jsonMap = new HashMap<String, Object>();
        jsonMap.put("total", dPage.getTotal());
        jsonMap.put("rows", dPage.getRecords());
        jsonMap.put("current", dPage.getCurrent());
        jsonMap.put("pages", dPage.getPages());
        responseModel.setStatus("0000");
        responseModel.setData(dPage);
        return  responseModel;
    }
}
